<?php

namespace App\Listeners\WorkOrder;

use App\Events\WorkOrder\WorkOrderFinishEvent;
use App\Listeners\BaseListener;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Models\WorkOrder\WorkOrderOperateLogModel;

class WorkOrderFinishListener extends BaseListener
{
    /**
     * @var int|float
     */
    public int $delay = 3 * 24 * 60 * 60;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param WorkOrderFinishEvent $workOrderFinishEvent
     * @return void
     */
    public function handle(WorkOrderFinishEvent $workOrderFinishEvent)
    {
        $workOrderApplyMain = WorkOrderApplyMainModel::query()
                                                     ->where("workOrderNo", $workOrderFinishEvent->workOrderNo)
                                                     ->first();

        if ($workOrderApplyMain->merchantConfirmFinishStatus != WorkOrderApplyMainModel::MERCHANT_CONFIRM_FINISH_STATUS_FINISH) {
            WorkOrderApplyMainModel::query()
                                   ->where("workOrderNo", $workOrderFinishEvent->workOrderNo)
                                   ->update([
                                       "merchantConfirmFinishType"   => WorkOrderApplyMainModel::MERCHANT_CONFIRM_FINISH_TYPE_SYSTEM,
                                       "merchantConfirmFinishTime"   => time(),
                                       "merchantConfirmFinishStatus" => WorkOrderApplyMainModel::MERCHANT_CONFIRM_FINISH_STATUS_FINISH
                                   ]);

            WorkOrderOperateLogModel::generateOperateLog(
                $workOrderApplyMain->workOrderNo,
                $workOrderApplyMain->step,
                WorkOrderOperateLogModel::TYPE_14,
                "系统",
                0,
                "系统确认完成工单"
            );
        }
    }
}
